﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Administration.Health;

namespace SPHealth.SharePoint.HealthRules.SP.Rules.Sys
{
    class ErrorFarmAccountUsedInteractively: SPHealthAnalysisRule
    {
        
        #region Locals
        // this assumes the timer job service is running as the farm account
        // which it should be...
        private string path_to_check = string.Format("{0}\\Contacts",Environment.GetEnvironmentVariable("USERPROFILE"));
        private string farm_account = string.Format("{0}\\{1}", Environment.UserDomainName, Environment.UserName);
        private SPHealthAnalysisRuleAutomaticExecutionParameters _executionParameters;
        #endregion
        
        #region Fields

        public override string Summary
        {
            get { return "The farm account is being used for interactive logon."; }
        }
        
        public override string Explanation
        {
            get { return string.Format("The farm account {0} appears to be being used for interactive logon. The farm account should only be used to run services and applications pools and not used for interactive logon.", farm_account); }
        }

        public override string Remedy
        {
            get { return "Do not use the farm account for interactive logon. Consider changing the account used as the farm account to remove any impacts caused by logging on interactively with the current farm account."; }
        }

        public override SPHealthCheckErrorLevel ErrorLevel
        {
            get { return SPHealthCheckErrorLevel.Error; }
        }

        public override SPHealthCategory Category
        {
            get { return SPHealthCategory.System; }
        }
                
        public override SPHealthAnalysisRuleAutomaticExecutionParameters AutomaticExecutionParameters
        {
            get
            {
                if (_executionParameters == null)
                {
                    _executionParameters = new SPHealthAnalysisRuleAutomaticExecutionParameters
                    {
                        Schedule = SPHealthCheckSchedule.Daily,
                        Scope = SPHealthCheckScope.All,
                        ServiceType = typeof(SPTimerService), //Tell SP if we want our own timer job or can we run with others...
                        RepairAutomatically = false
                    };
                }
                return _executionParameters;
            }
        }
        
        #endregion

        #region Methods

        public override SPHealthCheckStatus Check()
        {
            if (!SPFarm.Joined)
            {
                throw new InvalidOperationException();
            }
            
            if (System.IO.Directory.Exists(path_to_check))
                return SPHealthCheckStatus.Failed;

            return SPHealthCheckStatus.Passed;
        }

        #endregion
    }
}
